package com.company.project.common.job.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.entity.*;
import com.company.project.mapper.*;
import com.company.project.service.TCzjlService;
import com.company.project.service.TFzsczLogService;
import com.company.project.service.TSeatsService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 测试定时任务(演示Demo，可删除)
 * testTask为spring bean的名称， 方法名称必须是run
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Component("fzsczTask")
@Slf4j
public class FzsczTask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private TFzsczLogService tFzsczLogService;
	@Resource
	private TSeatsService tSeatsService;
	@Resource
	public TSeatsMapper tSeatsMapper;
	@Resource
	private SysUserMapper sysUserMapper;
	@Autowired
	public TThrbMapper thrbMapper;
	@Autowired
	private THwtjMapper hwtjMapper;
	@Autowired
	private TThjlmxMapper thjlmxMapper;
	@Autowired
	private THwtjFzsMapper hwtjFzsMapper;
	@Resource
	private TCzjlService tCzjlService;
	public void run(String params){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 30);
		List<TSeatsEntity> selectFzscz = tSeatsMapper.selectFzscz();
		for (TSeatsEntity ts : selectFzscz) {
			String accountid = ts.getAccountid();
			Integer residue = ts.getResidue();//剩余分钟
			Integer tcfzs = ts.getTcfzs();//套餐分钟数
//
//			//话务统计 t_hwtj
//			THwtjEntity hwtj = hwtjMapper.selectOneByCorporatename(ts.getCorporatename());
//			Date nowData = new Date();
//			if (nowData.equals(hwtj.getDate())) {
//				long timeInMillis = nowData.getTime(); // 转换为毫秒数
//				timeInMillis += 1000; // 加1秒（1000毫秒）
//				nowData.setTime(timeInMillis); // 更新时间
//			}
//			hwtj.setId("");
//			hwtj.setCompany(ts.getCorporatename());//公司名称
//			hwtj.setFzsze(hwtj.getFzsze()+tcfzs);//总分钟数
//			hwtj.setSyfzs(tcfzs);//剩余分钟数
//			hwtj.setBscs("0");
//			hwtj.setJts("0");
//			hwtj.setJfsc("0");//计费时长
//			hwtj.setThsc("0");//通话时长
//			hwtj.setXs(ts.getSalesname());//销售
//			hwtj.setOrgname(ts.getDept());//所属部门
//			hwtj.setLine(ts.getLine());//线路
//			hwtj.setDate(nowData);
//			String createuser = ts.getCreateuser();
//			if (!Func.equals("guest",createuser)){//非管理员创建 就是代理商
//				hwtj.setDl(createuser);//代理商
//			}
//			hwtjMapper.insert(hwtj);
//			log.info("话务统计:{}",hwtj.toString());

			//话务统计分钟数 t_hwtj_fzs
			THwtjFzsEntity fzsEntity = hwtjFzsMapper.selectOne(Wrappers.<THwtjFzsEntity>lambdaQuery().eq(THwtjFzsEntity::getCompany, ts.getCorporatename()));
			if (fzsEntity != null){
				fzsEntity.setFzsze(fzsEntity.getFzsze()+tcfzs);//分钟总数
				fzsEntity.setSyfzs(tcfzs);//剩余分钟数
				hwtjFzsMapper.updateById(fzsEntity);
			}
			SysUser sysUser = sysUserMapper.selectById(accountid);

			TFzsczLogEntity fzsczLog = new TFzsczLogEntity();
			fzsczLog.setUserid(sysUser.getUsername());
			fzsczLog.setGsmc(ts.getCorporatename());
			fzsczLog.setCztime(new Date());
			fzsczLog.setCzfzs(tcfzs);
			fzsczLog.setCzqfsyfzs(residue);

			ts.setResidue(tcfzs);
			ts.setTccztime(calendar.getTime());
			tSeatsMapper.updateById(ts);

			List<TSeatsEntity> tSeatsEntities = tSeatsMapper.selectList(Wrappers.<TSeatsEntity>lambdaQuery().eq(TSeatsEntity::getCorporatename, ts.getCorporatename()));
			for (TSeatsEntity t : tSeatsEntities) {
				t.setResidue(tcfzs);
				t.setTccztime(calendar.getTime());
				tSeatsMapper.updateById(t);
			}
			tFzsczLogService.save(fzsczLog);

			TCzjlEntity tCzjl = new TCzjlEntity();
			tCzjl.setUserid(sysUser.getUsername());//账号
			tCzjl.setGsmc(ts.getCorporatename());
			tCzjl.setCzTime(new Date());
			tCzjl.setFzs(tcfzs);
			tCzjl.setCzqk("套餐支出");
			tCzjlService.save(tCzjl);
		}

		logger.debug("FzsczTask定时任务正在执行，参数为：{}", params);

	}
}
